Group_user = function () {

}
Group_user.Name = "groupuser";
Group_user.datas = new Array();
Group_user.id = 0;
Group_user.getAll = function () {
    var url = Contants.Url + "groupuser";
    Group_user.datas = new Array();
    $.ajax({
        url: url,
        type: "POST",
        data: {fn: -1},
        dataType: "Json",
        cache: false,
        success: function (data) {            
            $("#body").empty();
            for (var i = 0; i < data.length; i++) {
                Group_user.datas.push(data[i]);
                var tr = "<tr>";
                tr += "<td>" + data[i].group_name + "</td>";
                tr += "<td class='center'>";
                tr += "<a class='btn btn-success' onclick = Group_user.Edit(" + i + ")>";
                tr += "<i class='icon-edit icon-white'></i> Edit</a>";
                tr += "<a class=\"btn btn-danger\" onclick = Group_user.Delete(" + data[i].id + ") >";
                tr += "<i class=\"icon-trash icon-white\"></i> Delete </a>";
                tr += "</td>";
                tr += "</tr>";
                $("#body").append(tr);
            }
        }
    });
}

Group_user.Edit = function(index){    
    if(index >= 0 && index < Group_user.datas.length){        
        var obj = Group_user.datas[index];
        var a = new Array();
        a.push(obj);
        Contants.Save(a,Group_user.Name);        
    }
    var url = Contants.Url + "editgroup.jsp";
    location.href = url;
    
}

Group_user.getItem = function(){
    var a = Contants.read(Group_user.Name);
    if(a.length > 0){
        var obj = a[0];
        Group_user.id = obj.id;
        $("#name").val(obj.group_name);
        $("#note").val(obj.note);
    }
}

Group_user.Insert = function(){
    var url = Contants.Url + "groupuser";
    var name = $("#name").val();
    var note = $("#note").val();
    var data = {fn: 0 ,
        name : name,
        note : note
    };
    $.ajax({
        url: url,
        type: "POST",
        data: data,
        dataType: "Json",
        cache: false,
        success: function (data) {            
            var t = parseInt(data,10);
            if(t > 0){                
                location.href = Contants.Url + "groupuser.jsp";
            }
        }
    });
}

Group_user.Update = function(){
    var url = Contants.Url + "groupuser";
    var name = $("#name").val();
    var note = $("#note").val();
    var data = {fn: 1 ,
        id : Group_user.id,
        name : name,
        note : note
    };
    $.ajax({
        url: url,
        type: "POST",
        data: data,
        dataType: "Json",
        cache: false,
        success: function (data) {            
            var t = parseInt(data,10);          
            location.href = Contants.Url + "groupuser.jsp";
        }
    });
}

Group_user.Delete = function(id){
    var url = Contants.Url + "groupuser";
    a = confirm(Contants.DeleteMessage);
    if(a == false){
        return;
    }
    var data = {fn: 2,
        id : id
    };
    $.ajax({
        url: url,
        type: "POST",
        data: data,
        dataType: "Json",
        cache: false,
        success: function (data) {            
           var t = parseInt(data,10);
           Group_user.getAll();
        }
    });
}
